package CCNQ::Proxy::routers;
# Copyright (C) 2006, 2007  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

#
# For more information visit http://carrierclass.net/
#
use strict; use warnings;

use base qw(CCNQ::Proxy::Base);

sub doc
{
    return <<'HTML';
    Routers should be the list of servers (IP addresses) that are part of
    the proxy cluster.
HTML
}

sub form
{
    my $self = shift;
    return (
        'IP' => 'ip',
        'Port' => 'integer',
    );
}

sub insert
{
    my $self = shift;
    my %params = @_;
    
    my $ip = $params{ip};
    my $port = $params{port};
    
    $port = 0 if not defined $port;

    return ()
        unless defined $ip and $ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
    
    return (<<'SQL',[$ip,$port]);
        INSERT INTO address(grp,ip_addr,mask,port) VALUES (0,?,32,?)
SQL
}

sub delete
{
    my $self = shift;
    my %params = @_;

    my $ip = $params{ip};
    my $port = $params{port};

    return (<<'SQL',[$ip,$port]);
        DELETE FROM address WHERE grp = 0 AND ip_addr = ? AND mask = 32 AND port = ?
SQL
}

sub list
{
    my $self = shift;
    return (<<'SQL',[],undef);
        SELECT ip_addr AS IP, port AS Port
        FROM address
        WHERE grp = 0 AND mask = 32
        ORDER BY ip_addr ASC
SQL
}

1;